<template>
  <view>
    <view v-for="(item, index) in goodsListData" :key="index" @click="targetGoodsDetail(item)"><my-goodsItem :goodsItem="item"></my-goodsItem></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      params: {
        query: '',
        cid: '',
        pagenum: 1, //页码
        pagesize: 10 //页面数据条数
      },
      goodsListData: [], //商品列表数据,
      total: 0, //总数据量
      isLoading: false //下拉触底节流阀 true代表运行中
    };
  },
  methods: {
    async getGoodsListData(back) {
      if ((this.params.pagenum - 1) * this.params.pagesize >= this.total && this.total > 0) return uni.$showMsg('没有更多了...');
      if (this.isLoading) return;
      this.isLoading = true;
      console.log('发起了请求');
      const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.params);
      this.isLoading = false;
      back && back();
      if (res.meta.status !== 200) return uni.$showMsg();
      this.params.pagenum++;
      this.goodsListData = [...this.goodsListData, ...res.message.goods];
      this.total = res.message.total;
    },
    targetGoodsDetail(item){
      uni.navigateTo({
        url:'../goods_detail/goods_detail?goods_id='+item.goods_id
      })
    }
  },
  onLoad(options) {
    this.params.query = options.query || '';
    this.params.cid = options.cid || '';
    this.getGoodsListData();
  },
  onReachBottom() {
    this.getGoodsListData();
  },
  onPullDownRefresh() {
    //重置数据
    this.params.pagenum = 1;
    this.goodsListData = [];
    this.total = 0;

    this.getGoodsListData(() => {
      uni.stopPullDownRefresh();
      console.log('执行了回调函数!');
    });
  }
};
</script>

<style lang="scss"></style>
